// 此模块为游戏的控制器模块
import UI from "./ui.js"; // 导入生成视图模块
import { isWin, playerMove } from "./play.js"; // 导入玩的模块

// 执行生成视图
UI();

// 游戏是否结束
let over = false;

window.addEventListener("keydown", function (e) {
    if (over) return false;
    let result = false;
    switch (e.key) {
        case "ArrowUp":
            result = playerMove("up");
            break;
        case "ArrowDown":
            result = playerMove("down");
            break;
        case "ArrowLeft":
            result = playerMove("left");
            break;
        case "ArrowRight":
            result = playerMove("right");
            break;
        default:
            break;
    }

    // 判断每成功移动一步后重新渲染页面且判断是否游戏结束
    if (result) {
        UI();
        if (isWin()) {
            over = true;
            setTimeout(() => {
                alert("游戏获得胜利");
            }, 100);
        }
    }
});
